using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Resources;

namespace template_dll.AssemblyResources
{
	internal sealed class Resources
	{
		#region Fields

		private static ResourceManager _stringsManager;

		private static object synchRoot = new object();

		#endregion

		#region Methods

		/// <summary>
		/// Returns the resource manager that string values are stored in.
		/// </summary>
		private static ResourceManager StringsManager
		{
			get
			{
				if ((_stringsManager == null))
				{
					lock (synchRoot)
					{
						if ((_stringsManager == null))
						{
							ResourceManager temp = new ResourceManager(typeof(Resources));
							Thread.MemoryBarrier();
							_stringsManager = temp;
						}
					}
				}
				return _stringsManager;
			}
		}

		#region Public methods

		/// <summary>
		/// Returns the formatted string resource identified by the specified key.
		/// </summary>
		/// <param name="key">The <see cref="StringsKey"/> to use to obtain the resource.</param>
		/// <param name="args">Any arguments that the string resource is expecting (resource string values should be fitted with one or more "{n}" format specifier place holder(s)).</param>
		/// <returns>The formatted string resource.</returns>
		public static string GetString(StringsKey key, params object[] args)
		{
			string value = GetString(key);
			return string.Format(CultureInfo.CurrentUICulture, value, args);
		}

		/// <summary>
		/// Returns StringsKey enum by specified enum name if it exists else returns StringsKey.StringIsNotDefined
		/// </summary>
		/// <param name="stringsKeyName"></param>
		/// <returns></returns>
		public static StringsKey GetVerifiedStringsKeyAt(string stringsKeyName)
		{
			if (Enum.IsDefined(typeof(StringsKey), stringsKeyName))
			{
				return (StringsKey)Enum.Parse(typeof(StringsKey), stringsKeyName);
			}
			else
			{
				Trace.WriteLine("StringsKey with name: " + stringsKeyName + " not found");
				return StringsKey.StringIsNotDefined;
			}
		}

		/// <summary>
		/// Returns the string resource identified by the specified key.
		/// </summary>
		/// <param name="key">The <see cref="StringsKey"/> to use to obtain the resource.</param>
		/// <returns>The string resource.</returns>
		public static string GetString(StringsKey key)
		{
			string keyValue = key.ToString();
			return StringsManager.GetString(keyValue, CultureInfo.CurrentUICulture);
		}

		#endregion

		#endregion
	}

	#region Resource Keys

	/// <summary>
	/// Defines the keys used to return string resources.
	/// </summary>
	internal enum StringsKey
	{
		StringIsNotDefined,

		ConnectionDialogSuccessfulConectionMessage,
		ConnectionDialogNoOneDatabaseMessage,

		OpenSaveFilesFilter,
		SaveLocalCubeFilesFilter,
		ExportFilesFilter,
		SaveSchemaFilesFilter,
		NotSupportedFileExtensionMessage,
		InvalidMenuItemMessage,
		InvalidToolBarButtonMessage,
		LoadThemeFilter,
	}

	#endregion
}